package br.com.rrf;

import java.io.Serializable;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ListView;

public class Fragment_tela_visualizar_pedido extends ListFragment
		implements OnClickListener{

	Item item;
	List<Item> listItens;
	ListView listViewPedidos;
	List<Pedido> pedidos;
	PedidoBD db;
	PedidoBD pedidoBD;
	Pedido pedido;
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			
		setHasOptionsMenu(true);
		View layout = inflater.inflate(R.layout.fragment_tela_visualizar_pedido, null);
		listViewPedidos = (ListView)layout.findViewById(R.id.listView1);
		
		pedidoBD = new PedidoBD(getActivity());
		
		return layout;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		db = new PedidoBD(getActivity());

		carregandoListItens();
		
		registerForContextMenu(getListView());
	}

	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	getActivity().getMenuInflater().inflate(R.menu.tela_visualizar_pedido, menu);
    	super.onCreateContextMenu(menu, v, menuInfo);
    }
	
    @Override
	public void onSaveInstanceState(Bundle outState) {  
     super.onSaveInstanceState(outState);  
     outState.putSerializable("Pedido", (Serializable) pedidos);
    }
    
    public void carregandoListItens() {
		pedidos = db.listarPedidos();
		AdapterTelaVisualizarPedido adapter = new AdapterTelaVisualizarPedido(pedidos);
		setListAdapter(adapter);
		
	}

	@Override
	public void onClick(View v) {
		
		
		
	}

	
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
	
			if (getActivity() instanceof PedidoClicado){
			((PedidoClicado)getActivity()).pedidoFoiClicado(pedidos.get(position));
		}
	}

}
